home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 296_01.zip / FLEXDEF.H < prev    next >
C/C++ Source or Header  |  1993-04-01  |  18KB  |  486 lines

  1. /*
  2.  *  Definitions for flex.
  3.  *
  4.  * modification history
  5.  * --------------------
  6.  * 02b kg, vp   30sep87  .added definitions for fast scanner; misc. cleanup
  7.  * 02a vp       27jun86  .translated into C/FTL
  8.  */
  9.  
  10. /*
  11.  * Copyright (c) 1987, the University of California
  12.  * 
  13.  * The United States Government has rights in this work pursuant to
  14.  * contract no. DE-AC03-76SF00098 between the United States Department of
  15.  * Energy and the University of California.
  16.  * 
  17.  * This program may be redistributed.  Enhancements and derivative works
  18.  * may be created provided the new works, if made available to the general
  19.  * public, are made available for use by anyone.
  20.  */
  21.  
  22. #include <stdio.h>
  23.  
  24. #include <string.h>
  25. #define bzero(s, n) memset((char *)(s), '\000', (unsigned)(n))
  26.  
  27. /* char *sprintf(); /* keep lint happy */
  28.  
  29.  
  30. /* maximum line length we'll have to deal with */
  31. #define MAXLINE BUFSIZ
  32.  
  33. /* maximum size of file name */
  34. #define FILENAMESIZE 1024
  35.  
  36. #define min(x,y) (x < y ? x : y)
  37. #define max(x,y) (x > y ? x : y)
  38.  
  39. #define true 1
  40. #define false 0
  41.  
  42.  
  43. #ifndef DEFAULT_SKELETON_FILE
  44. #define DEFAULT_SKELETON_FILE "flex.skel"
  45. #endif
  46.  
  47. #ifndef FAST_SKELETON_FILE
  48. #define FAST_SKELETON_FILE "flex.fastskel"
  49. #endif
  50.  
  51. /* special nxt[] action number for the "at the end of the input buffer" state */
  52. /* note: -1 is already taken by YY_NEW_FILE */
  53. #define END_OF_BUFFER_ACTION -3
  54. /* action number for default action for fast scanners */
  55. #define DEFAULT_ACTION -2
  56.  
  57. /* special chk[] values marking the slots taking by end-of-buffer and action
  58.  * numbers
  59.  */
  60. #define EOB_POSITION -1
  61. #define ACTION_POSITION -2
  62.  
  63. /* number of data items per line for -f output */
  64. #define NUMDATAITEMS 10
  65.  
  66. /* number of lines of data in -f output before inserting a blank line for
  67.  * readability.
  68.  */
  69. #define NUMDATALINES 10
  70.  
  71. /* transition_struct_out() definitions */
  72. #define TRANS_STRUCT_PRINT_LENGTH 15
  73.  
  74. /* returns true if an nfa state has an epsilon out-transition slot
  75.  * that can be used.  This definition is currently not used.
  76.  */
  77. #define FREE_EPSILON(state) \
  78.     (transchar[state] == SYM_EPSILON && \
  79.      trans2[state] == NO_TRANSITION && \
  80.      finalst[state] != state)
  81.  
  82. /* returns true if an nfa state has an epsilon out-transition character
  83.  * and both slots are free
  84.  */
  85. #define SUPER_FREE_EPSILON(state) \
  86.     (transchar[state] == SYM_EPSILON && \
  87.      trans1[state] == NO_TRANSITION) \
  88.  
  89. /* maximum number of NFA states that can comprise a DFA state.  It's real
  90.  * big because if there's a lot of rules, the initial state will have a
  91.  * huge epsilon closure.
  92.  */
  93. #define INITIAL_MAX_DFA_SIZE 750
  94. #define MAX_DFA_SIZE_INCREMENT 750
  95.  
  96. /* array names to be used in generated machine.  They're short because
  97.  * we write out one data statement (which names the array) for each element
  98.  * in the array.
  99.  */
  100.  
  101. #define ALIST 'l'    /* points to list of rules accepted for a state */
  102. #define ACCEPT 'a'    /* list of rules accepted for a state */
  103. #define ECARRAY 'e'    /* maps input characters to equivalence classes */
  104. #define MATCHARRAY 'm'    /* maps equivalence classes to meta-equivalence classes */
  105. #define BASEARRAY 'b'    /* "base" array */
  106. #define DEFARRAY 'd'    /* "default" array */
  107. #define NEXTARRAY 'n'    /* "next" array */
  108. #define CHECKARRAY 'c'    /* "check" array */
  109.  
  110. /* NIL must be 0.  If not, its special meaning when making equivalence classes
  111.  * (it marks the representative of a given e.c.) will be unidentifiable
  112.  */
  113. #define NIL 0
  114.  
  115. #define JAM -1    /* to mark a missing DFA transition */
  116. #define NO_TRANSITION NIL
  117. #define UNIQUE -1    /* marks a symbol as an e.c. representative */
  118. #define INFINITY -1    /* for x{5,} constructions */
  119.  
  120. /* size of input alphabet - should be size of ASCII set */
  121. #define CSIZE 127
  122.  
  123. #define INITIAL_MAXCCLS 100    /* max number of unique character classes */
  124. #define MAXCCLS_INCREMENT 100
  125.  
  126. /* size of table holding members of character classes */
  127. #define INITIAL_MAX_CCL_TBL_SIZE 500
  128. #define MAX_CCL_TBL_SIZE_INCREMENT 250
  129.  
  130. #define INITIAL_MNS 2000    /* default maximum number of nfa states */
  131. #define MNS_INCREMENT 1000    /* amount to bump above by if it's not enough */
  132.  
  133. #define INITIAL_MAX_DFAS 1000    /* default maximum number of dfa states */
  134. #define MAX_DFAS_INCREMENT 1000
  135.  
  136. #define JAMSTATE -32766    /* marks a reference to the state that always jams */
  137.  
  138. /* enough so that if it's subtracted from an NFA state number, the result
  139.  * is guaranteed to be negative
  140.  */
  141. #define MARKER_DIFFERENCE 32000
  142. #define MAXIMUM_MNS 31999
  143.  
  144. /* maximum number of nxt/chk pairs for non-templates */
  145. #define INITIAL_MAX_XPAIRS 2000
  146. #define MAX_XPAIRS_INCREMENT 2000
  147.  
  148. /* maximum number of nxt/chk pairs needed for templates */
  149. #define INITIAL_MAX_TEMPLATE_XPAIRS 2500
  150. #define MAX_TEMPLATE_XPAIRS_INCREMENT 2500
  151.  
  152. #define SYM_EPSILON 0    /* to mark transitions on the symbol epsilon */
  153.  
  154. #define INITIAL_MAX_SCS 40    /* maximum number of start conditions */
  155. #define MAX_SCS_INCREMENT 40    /* amount to bump by if it's not enough */
  156.  
  157. #define ONE_STACK_SIZE 500    /* stack of states with only one out-transition */
  158. #define SAME_TRANS -1    /* transition is the same as "default" entry for state */
  159.  
  160. /* the following percentages are used to tune table compression:
  161.  
  162.  * the percentage the number of out-transitions a state must be of the
  163.  * number of equivalence classes in order to be considered for table
  164.  * compaction by using protos
  165.  */
  166. #define PROTO_SIZE_PERCENTAGE 15
  167.  
  168. /* the percentage the number of homogeneous out-transitions of a state
  169.  * must be of the number of total out-transitions of the state in order
  170.  * that the state's transition table is first compared with a potential 
  171.  * template of the most common out-transition instead of with the first
  172.  * proto in the proto queue
  173.  */
  174. #define CHECK_COM_PERCENTAGE 50
  175.  
  176. /* the percentage the number of differences between a state's transition
  177.  * table and the proto it was first compared with must be of the total
  178.  * number of out-transitions of the state in order to keep the first
  179.  * proto as a good match and not search any further
  180.  */
  181. #define FIRST_MATCH_DIFF_PERCENTAGE 10
  182.  
  183. /* the percentage the number of differences between a state's transition
  184.  * table and the most similar proto must be of the state's total number
  185.  * of out-transitions to use the proto as an acceptable close match
  186.  */
  187. #define ACCEPTABLE_DIFF_PERCENTAGE 50
  188.  
  189. /* the percentage the number of homogeneous out-transitions of a state
  190.  * must be of the number of total out-transitions of the state in order
  191.  * to consider making a template from the state
  192.  */
  193. #define TEMPLATE_SAME_PERCENTAGE 60
  194.  
  195. /* the percentage the number of differences between a state's transition
  196.  * table and the most similar proto must be of the state's total number
  197.  * of out-transitions to create a new proto from the state
  198.  */
  199. #define NEW_PROTO_DIFF_PERCENTAGE 20
  200.  
  201. /* the percentage the total number of out-transitions of a state must be
  202.  * of the number of equivalence classes in order to consider trying to
  203.  * fit the transition table into "holes" inside the nxt/chk table.
  204.  */
  205. #define INTERIOR_FIT_PERCENTAGE 15
  206.  
  207. /* size of region set aside to cache the complete transition table of
  208.  * protos on the proto queue to enable quick comparisons
  209.  */
  210. #define PROT_SAVE_SIZE 2000
  211.  
  212. #define MSP 50    /* maximum number of saved protos (protos on the proto queue) */
  213.  
  214. /* maximum number of out-transitions a state can have that we'll rummage
  215.  * around through the interior of the internal fast table looking for a
  216.  * spot for it
  217.  */
  218. #define MAX_XTIONS_FOR_FULL_INTERIOR_FIT 4
  219.  
  220. /* number that, if used to subscript an array, has a good chance of producing
  221.  * an error; should be small enough to fit into a short
  222.  */
  223. #define BAD_SUBSCRIPT -32767
  224.  
  225. /* absolute value of largest number that can be stored in a short, with a
  226.  * bit of slop thrown in for general paranoia.
  227.  */
  228. #define MAX_SHORT 32766
  229.  
  230.  
  231. /* Declarations for global variables. */
  232.  
  233. /* variables for symbol tables:
  234.  * sctbl - start-condition symbol table
  235.  * ndtbl - name-definition symbol table
  236.  * ccltab - character class text symbol table
  237.  */
  238.  
  239. struct hash_entry
  240.     {
  241.     struct hash_entry *prev, *next;
  242.     char *name;
  243.     char *str_val;
  244.     int int_val;
  245.     } ;
  246.  
  247. typedef struct hash_entry *hash_table[];
  248.  
  249. #define NAME_TABLE_HASH_SIZE 101
  250. #define START_COND_HASH_SIZE 101
  251. #define CCL_HASH_SIZE 101
  252.  
  253. extern struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE]; 
  254. extern struct hash_entry *sctbl[START_COND_HASH_SIZE];
  255. extern struct hash_entry *ccltab[CCL_HASH_SIZE];
  256.  
  257.  
  258. /* variables for flags:
  259.  * printstats - if true (-v), dump statistics
  260.  * syntaxerror - true if a syntax error has been found
  261.  * eofseen - true if we've seen an eof in the input file
  262.  * ddebug - if true (-d), make a "debug" scanner
  263.  * trace - if true (-T), trace processing
  264.  * spprdflt - if true (-s), suppress the default rule
  265.  * interactive - if true (-I), generate an interactive scanner
  266.  * caseins - if true (-i), generate a case-insensitive scanner
  267.  * useecs - if true (-ce flag), use equivalence classes
  268.  * fulltbl - if true (-cf flag), don't compress the DFA state table
  269.  * usemecs - if true (-cm flag), use meta-equivalence classes
  270.  * reject - if true (-r flag), generate tables for REJECT macro
  271.  * fullspd - if true (-F flag), use Jacobson method of table representation
  272.  * gen_line_dirs - if true (i.e., no -L flag), generate #line directives
  273.  */
  274.  
  275. extern int printstats, syntaxerror, eofseen, ddebug, trace, spprdflt;
  276. extern int interactive, caseins, useecs, fulltbl, usemecs, reject;
  277. extern int fullspd, gen_line_dirs;
  278.  
  279.  
  280. /* variables used in the flex input routines:
  281.  * datapos - characters on current output line
  282.  * dataline - number of contiguous lines of data in current data
  283.  *    statement.  Used to generate readable -f output
  284.  * skelfile - fd of the skeleton file
  285.  * yyin - input file
  286.  * temp_action_file - temporary file to hold actions
  287.  * action_file_name - name of the temporary file
  288.  * infilename - name of input file
  289.  * linenum - current input line number
  290.  */
  291.  
  292. extern int datapos, dataline, linenum;
  293. extern FILE *skelfile, *yyin, *temp_action_file;
  294. extern char *infilename;
  295. extern char *action_file_name;
  296.  
  297.  
  298. /* variables for stack of states having only one out-transition:
  299.  * onestate - state number
  300.  * onesym - transition symbol
  301.  * onenext - target state
  302.  * onedef - default base entry
  303.  * onesp - stack pointer
  304.  */
  305.  
  306. extern int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
  307. extern int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
  308.  
  309.  
  310. /* variables for nfa machine data:
  311.  * current_mns - current maximum on number of NFA states
  312.  * accnum - number of the last accepting state
  313.  * firstst - physically the first state of a fragment
  314.  * lastst - last physical state of fragment
  315.  * finalst - last logical state of fragment
  316.  * transchar - transition character
  317.  * trans1 - transition state
  318.  * trans2 - 2nd transition state for epsilons
  319.  * accptnum - accepting number
  320.  * lastnfa - last nfa state number created
  321.  */
  322.  
  323. extern int current_mns;
  324. extern int accnum, *firstst, *lastst, *finalst, *transchar;
  325. extern int *trans1, *trans2, *accptnum, lastnfa;
  326.  
  327.  
  328. /* variables for protos:
  329.  * numtemps - number of templates created
  330.  * numprots - number of protos created
  331.  * protprev - backlink to a more-recently used proto
  332.  * protnext - forward link to a less-recently used proto
  333.  * prottbl - base/def table entry for proto
  334.  * protcomst - common state of proto
  335.  * firstprot - number of the most recently used proto
  336.  * lastprot - number of the least recently used proto
  337.  * protsave contains the entire state array for protos
  338.  */
  339.  
  340. extern int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
  341. extern int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
  342.  
  343.  
  344. /* variables for managing equivalence classes:
  345.  * numecs - number of equivalence classes
  346.  * nextecm - forward link of Equivalence Class members
  347.  * ecgroup - class number or backward link of EC members
  348.  * nummecs - number of meta-equivalence classes (used to compress
  349.  *   templates)
  350.  * tecfwd - forward link of meta-equivalence classes members
  351.  * tecbck - backward link of MEC's
  352.  */
  353.  
  354. extern int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs;
  355. extern int tecfwd[CSIZE + 1], tecbck[CSIZE + 1];
  356.  
  357.  
  358. /* variables for start conditions:
  359.  * lastsc - last start condition created
  360.  * current_max_scs - current limit on number of start conditions
  361.  * scset - set of rules active in start condition
  362.  * scbol - set of rules active only at the beginning of line in a s.c.
  363.  * scxclu - true if start condition is exclusive
  364.  * actvsc - stack of active start conditions for the current rule
  365.  */
  366.  
  367. extern int lastsc, current_max_scs, *scset, *scbol, *scxclu, *actvsc;
  368.  
  369.  
  370. /* variables for dfa machine data:
  371.  * current_max_dfa_size - current maximum number of NFA states in DFA
  372.  * current_max_xpairs - current maximum number of non-template xtion pairs
  373.  * current_max_template_xpairs - current maximum number of template pairs
  374.  * current_max_dfas - current maximum number DFA states
  375.  * lastdfa - last dfa state number created
  376.  * nxt - state to enter upon reading character
  377.  * chk - check value to see if "nxt" applies
  378.  * tnxt - internal nxt table for templates
  379.  * base - offset into "nxt" for given state
  380.  * def - where to go if "chk" disallows "nxt" entry
  381.  * tblend - last "nxt/chk" table entry being used
  382.  * firstfree - first empty entry in "nxt/chk" table
  383.  * dss - nfa state set for each dfa
  384.  * dfasiz - size of nfa state set for each dfa
  385.  * dfaacc - accepting set for each dfa state (or accepting number, if
  386.  *    -r is not given)
  387.  * accsiz - size of accepting set for each dfa state
  388.  * dhash - dfa state hash value
  389.  * todo - queue of DFAs still to be processed
  390.  * todo_head - head of todo queue
  391.  * todo_next - next available entry on todo queue
  392.  * numas - number of DFA accepting states created; note that this
  393.  *    is not necessarily the same value as accnum, which is the analogous
  394.  *    value for the NFA
  395.  * numsnpairs - number of state/nextstate transition pairs
  396.  * jambase - position in base/def where the default jam table starts
  397.  * jamstate - state number corresponding to "jam" state
  398.  * end_of_buffer_state - end-of-buffer dfa state number
  399.  */
  400.  
  401. extern int current_max_dfa_size, current_max_xpairs;
  402. extern int current_max_template_xpairs, current_max_dfas;
  403. extern int lastdfa, lasttemp, *nxt, *chk, *tnxt;
  404. extern int *base, *def, tblend, firstfree, **dss, *dfasiz;
  405. extern union dfaacc_union
  406.     {
  407.     int *dfaacc_set;
  408.     int dfaacc_state;
  409.     } *dfaacc;
  410. extern int *accsiz, *dhash, *todo, todo_head, todo_next, numas;
  411. extern int numsnpairs, jambase, jamstate;
  412. extern int end_of_buffer_state;
  413.  
  414. /* variables for ccl information:
  415.  * lastccl - ccl index of the last created ccl
  416.  * current_maxccls - current limit on the maximum number of unique ccl's
  417.  * cclmap - maps a ccl index to its set pointer
  418.  * ccllen - gives the length of a ccl
  419.  * cclng - true for a given ccl if the ccl is negated
  420.  * cclreuse - counts how many times a ccl is re-used
  421.  * current_max_ccl_tbl_size - current limit on number of characters needed
  422.  *    to represent the unique ccl's
  423.  * ccltbl - holds the characters in each ccl - indexed by cclmap
  424.  */
  425.  
  426. extern int lastccl, current_maxccls, *cclmap, *ccllen, *cclng, cclreuse;
  427. extern int current_max_ccl_tbl_size;
  428. extern char *ccltbl;
  429.  
  430.  
  431. /* variables for miscellaneous information:
  432.  * starttime - real-time when we started
  433.  * endtime - real-time when we ended
  434.  * nmstr - last NAME scanned by the scanner
  435.  * sectnum - section number currently being parsed
  436.  * nummt - number of empty nxt/chk table entries
  437.  * hshcol - number of hash collisions detected by snstods
  438.  * dfaeql - number of times a newly created dfa was equal to an old one
  439.  * numeps - number of epsilon NFA states created
  440.  * eps2 - number of epsilon states which have 2 out-transitions
  441.  * num_reallocs - number of times it was necessary to realloc() a group
  442.  *          of arrays
  443.  * tmpuses - number of DFA states that chain to templates
  444.  * totnst - total number of NFA states used to make DFA states
  445.  * peakpairs - peak number of transition pairs we had to store internally
  446.  * numuniq - number of unique transitions
  447.  * numdup - number of duplicate transitions
  448.  * hshsave - number of hash collisions saved by checking number of states
  449.  */
  450.  
  451. extern char *starttime, *endtime, nmstr[MAXLINE];
  452. extern int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
  453. extern int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
  454.  
  455. char *allocate_array(), *reallocate_array();
  456.  
  457. #define allocate_integer_array(size) \
  458.     (int *) allocate_array( size, sizeof( int ) )
  459.  
  460. #define reallocate_integer_array(array,size) \
  461.     (int *) reallocate_array( (char *) array, size, sizeof( int ) )
  462.  
  463. #define allocate_integer_pointer_array(size) \
  464.     (int **) allocate_array( size, sizeof( int * ) )
  465.  
  466. #define allocate_dfaacc_union(size) \
  467.     (union dfaacc_union *) \
  468.         allocate_array( size, sizeof( union dfaacc_union ) )
  469.  
  470. #define reallocate_integer_pointer_array(array,size) \
  471.     (int **) reallocate_array( (char *) array, size, sizeof( int * ) )
  472.  
  473. #define reallocate_dfaacc_union(array, size) \
  474.     (union dfaacc_union *)  reallocate_array( (char *) array, size, sizeof( union dfaacc_union ) )
  475.  
  476. #define allocate_character_array(size) allocate_array( size, sizeof( char ) )
  477.  
  478. #define reallocate_character_array(array,size) \
  479.     reallocate_array( array, size, sizeof( char ) )
  480.  
  481.  
  482. /* used to communicate between scanner and parser.  The type should really
  483.  * be YYSTYPE, but we can't easily get our hands on it.
  484.  */
  485. extern int yylval;
  486.